package top.aigoo.project01.interceptor.shop;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import top.aigoo.project01.entity.PersonInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @Description: 店家管理系统登录验证拦截器
 * @Author: Mr.OYSHUO.欧阳大叔  mail:1642302522@qq.com
 * @Version 1.0.0
 * @Date: 2021/9/21 14:05
 */
public class ShopLoginInterceptor extends HandlerInterceptorAdapter {
    /*做事之前拦截，用户操作发生前，改写PreHandle里面的逻辑，进行拦截*/
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        //从session中取出来用户信息
        Object userObj = request.getSession().getAttribute("user");
        if (userObj!=null){
            PersonInfo user = (PersonInfo) userObj;
            if (user!=null
                    && user.getUserId()!=null
                    && user.getUserId()>0
                    && user.getEnableStatus() == 1){
                //通过验证返回true,拦截器返回true之后，用户接下来的操作就得以正常进行
                return true;
            }

        }
        //如果不满足登录验证，则直接跳转到账号登录页面
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<script>");
        out.println("window.open ('" + request.getContextPath()
                + "/local/login?usertype=2','_self')");
        out.println("</script>");
        out.println("</html>");
        return false;
    }

}
